package cn.xorange.commons.data.core.entity;

import cn.xorange.commons.data.annotation.MColumn;
import cn.xorange.commons.data.entity.MyBatisParam;
import cn.xorange.commons.data.entity.Order;
import cn.xorange.commons.data.entity.Where;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Map;
import java.util.Objects;

/**
 * @author : yangjian
 * date : 2021/8/18
 */
public class BaseEntity implements Serializable {
    public BaseEntity(){
        this.where = new Where(this.getClass());
        this.order = new Order();
        this.queryWrapper = new QueryWrapper<>(this);
    }

    @MColumn()
    private String delFlag;

    @JsonIgnore
    @JSONField(serialize = false)
    private String tableName;

    /** 请求参数 */
    private JSONObject params;

    @JsonIgnore
    @JSONField(serialize = false)
    private String[] selectColumn;
    @JsonIgnore
    @JSONField(serialize = false)
    private String[] excludeColumn;

    @JsonIgnore
    @JSONField(serialize = false)
    private Integer selectLimit;

    @JsonIgnore
    @JSONField(serialize = false)
    public MyBatisParam p;
    @JsonIgnore
    @JSONField(serialize = false)
    public Where where;
    @JsonIgnore
    @JSONField(serialize = false)
    public Order order;
    @JsonIgnore
    @JSONField(serialize = false)
    public QueryWrapper<?> queryWrapper;
    /** sql 过滤条件 */
    @JsonIgnore
    @JSONField(serialize = false)
    private String sqlFilter;


    public QueryWrapper<?> getQueryWrapper() {
        return queryWrapper;
    }

    public String getSqlFilter() {
        return sqlFilter;
    }
    public void setSqlFilter(String sqlFilter) {
        this.sqlFilter = sqlFilter;
    }

    public void selectColumn(String... fieldName){
        this.selectColumn = fieldName;
    }
    public String[] getSelectColumn(){
        return this.selectColumn;
    }
    public void setExcludeColumn(String... fieldName){this.excludeColumn = fieldName; }
    public String[] getExcludeColumn(){ return this.excludeColumn; }

    public void setTableName(String tableName){
        this.tableName = tableName;
    }
    public String getTableName(){
        return this.tableName;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public JSONObject getParams(){
        if (params == null) {
            params = new JSONObject();
        }
        return this.params;
    }
    public void setParams(JSONObject jsonObject){
        this.params = jsonObject;
    }
    public void setParams(Map<String,Object> paramsMap){
        if(Objects.nonNull(paramsMap)){
            if (this.params == null){
                this.params = new JSONObject();
            }
            this.params.putAll(paramsMap);
        }
    }
    public void setParams(String fieldName,Object obj){
        if (this.params == null){
            this.params = new JSONObject();
        }
        this.params.put(fieldName,obj);
    }

    public Integer getSelectLimit(){
        return this.selectLimit;
    }
    public void setSelectLimit(Integer selectLimit) {
        this.selectLimit = selectLimit;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public String getDelFlag(){
        return this.delFlag;
    }
    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

}
